const app = getApp()
var cf = require('../../lib/config.js');
var c = require('../../lib/common.js');
var login = require('../../lib/login.js');
Page({
  data: {
    fbsongyangsongyangzhongxin:[],
    region: ['广东省', '全国', '海珠区'],
    fbsongyang: [],
    height: 240,
    topname: ' ',
    session: null,
    hidestatus: true,
    msgList: null,
    seecw: null,
    seepeople: null,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imgUrls: [
      '/images/banner/banner.jpg'
    ],
    prepage: 0,
    index: '',
    address: '',
    pagenum: 0,
    tid: 1,
    searchinput: '',
    temhidden: true,
    hidden: true,
    leftname:'首页',
    action:'action',
    noselct:'noselct',
    ttid:1
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  temclose: function() {
    this.setData({
      temhidden: true
    })
  },
  onPageScroll: function(e) {

    var fiexcss = this.data.fiexcss;
    var scrollTop = e.scrollTop;
    if (scrollTop >= 200) {
      if (fiexcss == 1) {
        return;
      }
      this.setData({
        scrollTop: e.scrollTop,
        fiexcss: 1
      })
    } else {
      if (fiexcss == 0) {
        return;
      }
      this.setData({
        scrollTop: e.scrollTop,
        fiexcss: 0
      })
    }
    // var scrollTopheight=e.scrollTop;
    // if (scrollTopheight>=300){
    //  this.setData({fiexcss:1})
    // }else{
    //  this.setData({fiexcss:0})
    // }
  },


  inputSearch: function() {

    var tid = this.data.tid;
    var inputSearch = this.data.searchinput;
    console.log(tid);
    console.log(inputSearch);
    this.setData({
      fbsongyang: []
    })
    if (tid == 1) {
      this.getFbSong('', '', 0, inputSearch);
    } else if (tid == 3 || tid == 4) {
      this.seePet('', '', 0, tid, inputSearch);
    }
  },
  onLoad: function() {
    this.getFbSong('', '', 0, '');
    this.fbsongyanglyinfo();
  },
  //等待领养
  getFbSong(index, address, pagenum, inputsearch) {
    var that = this;
    wx.request({
      url: 'https://www.cwlyz.com/wx/Index/lylist',
      data: {
        pagenum: pagenum,
        area: address,
        index: index,
        inputsearch: inputsearch
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        console.log(res);
        that.setData({
          hidestatus: false
        });
        var fbsongyang = that.data.fbsongyang;
        var sysinfo = res.data.data;
        var fbsongyangnew = fbsongyang.concat(sysinfo);
        console.log(fbsongyangnew[0]);
        if (fbsongyangnew[0] == null) {
          var fbsongyangnew = '';
        }
        that.setData({
          fbsongyang: fbsongyangnew,
          nextpage: res.data.nextpage
        });

      }
    })
  },
  //寻宠启示
  seePet: function(index, address, pagenum, typeinfo, inputSearch) {
     
    if (typeof (inputSearch) =='undefined'){
      var inputSearch= '';
    }

    var that = this;
    wx.request({
      url: 'https://www.cwlyz.com/wx/Index/seepetlist',
      data: {
        index: index,
        area: address,
        pagenum: pagenum,
        typeinfo: typeinfo,
        inputSearch: inputSearch
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        var fbsongyang = that.data.fbsongyang;
        var sysinfo = res.data.data;
        var fbsongyangnew = fbsongyang.concat(sysinfo);
        if (fbsongyangnew[0] == null) {
          var fbsongyangnew = '';
        }
        console.log(fbsongyangnew);
        that.setData({
          nextpage: res.data.nextpage,
          fbsongyang: fbsongyangnew
        });
      }
    })
  },
  searchinput: function(e) {
    var value = e.detail.value;
    this.setData({
      searchinput: value
    })
  },
  bindRegionChange: function(e) { //城市选择
    var tid = this.data.tid;
    var address = e.detail.value;
    this.setData({
      address: address[1],
      region: address,
      fbsongyang: []
    })
    console.log(tid);
    if (tid == 1) {
      this.getFbSong(0, address[1], 0, '');
    } else if (tid == 4 || tid == 3) {
      console.log('我是3或者4');
      console.log(address[1]);
      this.seePet(0, address[1], 0, tid);
    }
  },
  petclass: function(e) {
    this.setData({
      fbsongyang: []
    })
    var tid = this.data.tid;
    var index = e.currentTarget.dataset.pindex;
    var address = this.data.address;
    this.setData({
      index: index
    })
    if (tid == 1) {
      this.getFbSong(index, address, 0, '');
    } else if (tid == 2 || tid == 3) {
      this.seePet(index, address, 0, tid);
    }

  },
  fbsongyanglyinfo: function() {
    var that = this;
    c.ajax(
      cf.hostapi + 'Index/fbsongyanglyinfo', {},
      res => {
        console.log(res.data.data);
        that.setData({
          msgList: res.data.data
        })
      }
    )
  },
  getUserInfo: function(e) {
    login.login(e, this);
    this.setData({
      temhidden: false
    })
  },
  bindinput: function() {

  },
  onPullDownRefresh: function() {
    var that = this;
    wx.request({
      url: 'https://www.cwlyz.com/wx/Index/index', //仅为示例，并非真实的接口地址
      data: {
        x: '',
        y: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        that.setData({
          hidestatus: false
        });
        that.setData({
          fbsongyang: res.data.data['0']
        });
        that.setData({
          msgList: res.data.data['1']
        });
        that.setData({
          seecw: res.data.data['2']
        });
        that.setData({
          seepeople: res.data.data['3']
        });
        wx.stopPullDownRefresh();
      }
    })

  },
  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '宠物领养-宠物送养',
      path: '/pages/index/index',
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },
  loadbinda: function() {
    console.log(1111);
  },
  onShow: function() {
    var uid = wx.getStorageSync('userinfo').id;
    if (uid) {
      var jim = app.globalData.jimnew;
      var isLogin = jim.isLogin();
      var isInit = jim.isInit();
      console.log(isLogin);
      console.log(isInit);
      if (isLogin || isInit) {
        // this.getConversation();
      } else {
        login.iminit('imlogin', uid);
      }
    }
    c.loginVer(this);
  },
  imgclick: function(e) {
    var sytype = e.currentTarget.dataset.status;
    if (sytype == 2) {
      wx.showToast({
        title: '已经送养了！',
        image: '/images/meiyou.png'
      })
      return;
    }
    if (sytype == 0) {
      wx.showToast({
        title: '正在审核中...',
        image: '/images/shzt.png'
      })
      return;
    }
  },
  listinfo: function(e) {
    var syid = e.currentTarget.dataset.syid;
    var sytype = e.currentTarget.dataset.sytype;
    if (sytype == 2) {
      wx.showToast({
        title: '已经送养了！',
        image: '/images/meiyou.png'
      })
      return;
    }
    if (sytype == 0) {
      wx.showToast({
        title: '正在审核中...',
        image: '/images/shzt.png'
      })
      return;
    }
    if (sytype == 1) {
      wx.navigateTo({
        url: '/pages/details/details?id=' + syid,
      })
    }
  },
  moreinfos: function() { //点击更多
    var tid = this.data.tid;
    var index = this.data.index;
    var address = this.data.address;
    var pagenum = this.data.pagenum + 1;
    var inputSearch = this.data.searchinput;
    this.setData({
      pagenum: pagenum
    })
    if (tid == 1) {
      this.getFbSong(index, address, pagenum, inputSearch);
    } else if (tid == 3 || tid == 4) {
      this.seePet(index, address, pagenum, tid, inputSearch);
    }else{
      this.songyangzhongxin(index, address, pagenum);
    }
  },
  topmenuclick: function(e) {
    var inputSearch = this.data.searchinput;
    var index = this.data.index;
    var address = this.data.address;
    var tid = e.currentTarget.dataset.tid;
    var ttid = tid;
    console.log('tid');
    console.log(tid);
    this.setData({
      ttid: ttid,
      tid: tid,
      fbsongyang: [],
      pagenum: 0,
      index: 0,
      inputSearch: inputSearch
    })
    console.log(9090);
    console.log(this.data);
    if (tid == 1) {
      this.getFbSong(0, address, 0, inputSearch);
    } else if (tid == 3 || tid == 4) {
      this.seePet(0, address, 0, tid, inputSearch);
    } else if (tid == 2) {
      console.log(123);
      this.songyangzhongxin();
    }


  },
  //宠物咨询
  getPetInfo() {
    var newslist=[];
    c.ajax(
      cf.hostapi + 'NewsInfo/GetNewsList', {},
      res => {
        console.log(res.data.data);
        this.setData({ newslist: res.data.data})
        
      }
    )
  },
  //小程序跳转
  jumpshequ: function () {
    console.log('jumpshequ');
    wx.navigateToMiniProgram({
      appId: 'wxbfff7aba15880236',
      path: 'pages/index/index',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release',
      success(res) {
        console.log(res);
      },
      fail(res) {
        
      }
    })
  },
  clickreadid:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/newsDetails/newsDetails?id=' + id,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
 //送养中心
  songyangzhongxin: function (index,address,pagenum){
    
    var that = this;
    wx.request({
      url: 'https://www.cwlyz.com/wx/Index/wantlylist',

      data: {
        pagenum: pagenum,
        area: address,
        index: index,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var fbsongyang = that.data.fbsongyangsongyangzhongxin;
        var sysinfo = res.data.data;
        var fbsongyangnew = fbsongyang.concat(sysinfo);
        console.log('fbsongyangnew');
        console.log(fbsongyangnew);
        that.setData({ fbsongyangsongyangzhongxin: fbsongyangnew });
      }
    })
  }

})